package de.nak.roommgmt.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;

import de.nak.roommgmt.model.Room;
import de.nak.roommgmt.service.RoomService;

/**
 * Action that shows the list of rooms.
 * 
 * @author Stephan Anft
 */
public class ShowRoomListAction extends ActionSupport {

	/** {@inheritDoc} */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// Get the service and load the room set
		WebApplicationContext ctx = getWebApplicationContext();
		RoomService roomService = (RoomService)ctx.getBean("roomService");
		List<Room> rooms = roomService.listRooms();
		// Store value in form
		RoomForm roomListForm = (RoomForm) form;
		roomListForm.setRooms(rooms);
		// Return result
		return mapping.findForward("success");
	}
}
